package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.properties.TcConfig;
import cn.stylefeng.guns.sys.modular.system.entity.ItemInterface;
import cn.stylefeng.guns.sys.modular.system.mapper.ItemInterfaceMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.ItemBusUnitParam;
import cn.stylefeng.guns.sys.modular.system.model.params.ItemInterfaceParam;
import cn.stylefeng.guns.sys.modular.system.model.result.ItemInterfaceResult;
import cn.stylefeng.guns.sys.modular.system.service.ItemInterfaceService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 品项表 服务实现类
 * </p>
 *
 * @author jiangge
 * @since 2020-09-13
 */
@Service
public class ItemInterfaceServiceImpl extends ServiceImpl<ItemInterfaceMapper, ItemInterface> implements ItemInterfaceService {
    private Logger logger = LoggerFactory.getLogger(ItemInterfaceServiceImpl.class);
    private RestTemplate restTemplate = new RestTemplate();

    @Autowired
    private TcConfig tcConfig;

    @Override
    public void add(ItemInterfaceParam param) {
        ItemInterface entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ItemInterfaceParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(ItemInterfaceParam param) {
        ItemInterface oldEntity = getOldEntity(param);
        ItemInterface newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ItemInterfaceResult findBySpec(ItemInterfaceParam param) {
        return null;
    }

    @Override
    public List<ItemInterfaceResult> findListBySpec(ItemInterfaceParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ItemInterfaceParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public void initIntemInferfaceData() {
        String url = "http://btyjt.c3.fxscm.net/cldpoint/getItemInterface.do?ent=ENT7617&username="+tcConfig.getUsername()+"&password="+tcConfig.getPassword();
        String content = restTemplate.getForObject(url, String.class);
        JSONObject jsonObjectResult = JSON.parseObject(content);

        //数据转换
        List<ItemInterface> itemInterfaceList = JSON.parseArray(jsonObjectResult.getString("data"), ItemInterface.class);

        if (!CollectionUtils.isEmpty(itemInterfaceList)) {
            //重新保存之前先把之前所有的删除
            this.baseMapper.truncateData();
            for (ItemInterface itemUnit : itemInterfaceList) {
                this.saveOrUpdate(itemUnit);
            }
        }

        logger.info("初始化品项数据：{}条数据", itemInterfaceList.size());

    }

    /***
     * 获取所有品项单位数据
     * @return
     */
    @Override
    public List<ItemBusUnitParam> getAllItemBusUnit() {
        return this.baseMapper.getAllItemBusUnit();
    }

    @Override
    public List<ItemInterfaceResult> findListByParam(ItemInterfaceParam itemInterfaceParam) {
        List<ItemInterfaceResult> list = this.baseMapper.findListByParam(itemInterfaceParam);
        return list;
    }

    @Override
    public List<ItemInterfaceResult> findSpecifiedItemlist(ItemInterfaceParam itemInterfaceParam) {
        List<ItemInterfaceResult> list = this.baseMapper.findSpecifiedItemlist(itemInterfaceParam);
        return list;
    }

    private Serializable getKey(ItemInterfaceParam param) {
        return null;
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ItemInterface getOldEntity(ItemInterfaceParam param) {
        return this.getById(getKey(param));
    }

    private ItemInterface getEntity(ItemInterfaceParam param) {
        ItemInterface entity = new ItemInterface();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }


}
